import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent, STReq, STRes, STPage, STColumnTag } from '@delon/abc';
import { SFSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd';
import { CommonService } from '@core/service/common.service';
import { SysRoleRoleModalComponent } from './role-modal/role-modal.component';

@Component({
  selector: 'app-sys-role',
  templateUrl: './role.component.html',
})
export class SysRoleComponent implements OnInit {
  url = '';
  req: STReq = {
    reName: { pi: 'page', ps: 'size' },
  };
  res: STRes = {
    reName: { total: 'data.total', list: 'data.list' },
  };
  page: STPage = {
    zeroIndexed: true,
  };
  searchSchema: SFSchema = {
    properties: {
      name: {
        type: 'string',
        title: '角色名称',
      },
      roleCode: {
        type: 'string',
        title: '角色编码',
      }
    },
  };
  @ViewChild('st') st: STComponent;
  columns: STColumn[] = [
    { title: '角色名称', index: 'name' },
    { title: '角色编码', index: 'roleCode' },
    {
      title: '操作',
      buttons: [{
          icon: 'edit',
          type: 'modal',
          modal: {
            component: SysRoleRoleModalComponent
          },
          click: () => this.st.reload(),
        },
        { icon: 'delete', type: 'del', click: (r) => this.doDel(r) },
      ],
    },
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    public msg: NzMessageService,
    public commonService: CommonService,
  ) {}

  ngOnInit() {
    this.url = `${this.commonService.getServerPath()}api/role`;
  }

  add() {
    this.modal
      .createStatic(SysRoleRoleModalComponent, { i: {} })
      .subscribe(() => {
        this.st.reload();
      });
  }

  doDel(r) {
    this.http.delete(this.url + '/' + r.id).subscribe(
      succ => {
        this.st.reload();
        this.msg.info('删除成功');
      },
      succ => {
        this.msg.info('删除失败');
      },
    );
  }

}
